﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
//using BSCMFF.Web.Pages.UserPages;
using BSCMFF.Web.UserControls.Details;
using BSCMFF.Core.Exceptions.Database;
using BSCMFF.Web.Pages.UserPages;

namespace BSCMFF.Web.UserControls.EmployeeTabs
{
    public partial class TasksTab : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RadGrid1.Culture = System.Threading.Thread.CurrentThread.CurrentCulture;
            //if (this.Page.GetType().BaseType.Name == "EmployeeInfo")
            //{
            //    int userID = ((EmployeeInfo)this.Page).SelectedUserID;

            //    BSCEntities context = new BSCEntities();

            //    List<Task> tasks = (from task in context.Tasks
            //                        where task.User_FK == userID
            //                        select task).ToList();
            //    RadGrid1.DataSource = tasks;
            //    RadGrid1.DataBind();

            //}

            string formatDateString;
            if (Page.UICulture == "English (United States)") //"en-US")
            {
                formatDateString = "{0:MM/dd/yyyy}";
            }
            else
            {
                formatDateString = "{0:dd.MM.yyyy}";
            }

            GridBoundColumn dateAssigned = (GridBoundColumn)RadGrid1.Columns.FindByUniqueName("Date_Assigned");
            dateAssigned.DataFormatString = formatDateString; //= ComboDates.SelectedValue;
            GridBoundColumn dateEndExpected = (GridBoundColumn)RadGrid1.Columns.FindByUniqueName("Date_EndExpected");
            dateEndExpected.DataFormatString = formatDateString;
            GridBoundColumn dateCompleted = (GridBoundColumn)RadGrid1.Columns.FindByUniqueName("Date_Completed");
            dateCompleted.DataFormatString = formatDateString;
        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            TaskDetails userControl = (TaskDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);

            BSCEntities context = new BSCEntities();

            Task t = new Task();
            t.Name = userControl.Name;
            t.Description = userControl.Description;
            t.Weight = userControl.Weight;
            t.Priority = userControl.Priority;
            t.Date_Assigned = userControl.Assigned;
            t.Date_EndExpected = userControl.Expected;
            t.Date_Completed = userControl.Completed;

            if (((EmployeeInfo)this.Page).SelectedUserID != 0)
            {
                t.User_FK = ((EmployeeInfo)this.Page).SelectedUserID;
            }
            
            context.Tasks.AddObject(t);

            try
            {
                context.SaveChanges();
            }
            catch
            {
                throw new InconsistentDatabaseException("Couldn't create new task");
            }

        }

        protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl taskDetail = (UserControl)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            int task_ID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Task_ID"].ToString());

            BSCEntities context = new BSCEntities();
            var task = (from t in context.Tasks
                        where t.Task_ID == task_ID
                        select t).SingleOrDefault();

            if (task != null)
            {
                context.DeleteObject(task);
                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't delete task");
                }
            }
        }

        protected void RadGrid1_UpdateCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            TaskDetails userControl = (TaskDetails)e.Item.FindControl(GridEditFormItem.EditFormUserControlID);


            int id = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Task_ID"].ToString());
            BSCEntities context = new BSCEntities();

            Task t = (from task in context.Tasks
                      where task.Task_ID == id
                      select task).SingleOrDefault();

            if (t != null)
            {

                t.Name = userControl.Name;
                t.Description = userControl.Description;
                t.Weight = userControl.Weight;
                t.Priority = userControl.Priority;
                t.Date_Assigned = userControl.Assigned;
                t.Date_EndExpected = userControl.Expected;
                t.Date_Completed = userControl.Completed;

                if (((EmployeeInfo)this.Page).SelectedUserID != 0)
                {
                    t.User_FK = ((EmployeeInfo)this.Page).SelectedUserID;
                }

                try
                {
                    context.SaveChanges();
                }
                catch
                {
                    throw new InconsistentDatabaseException("Couldn't update task");
                }
            }
        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridDataItem)
            //{
            //    GridDataItem dataItem = e.Item as GridDataItem;
            //    TableCell dateAssigned = dataItem["Date_Assigned"];
            //    dateAssigned.
                
            //    int fieldValue = int.Parse(dataItem["Weight"].Text);
            //    //totalWeight += fieldValue;
            //}
        }
    }
}